package com.sauro.common.util;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

public class MD5Util {

    public static String getMd5(String s) {
        if (StringUtils.isEmpty(s) || StringUtils.isBlank(s)) {
            return null;
        }

        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static String generateEncryptionPwd(String pwd) {
        byte[] bytePwd = pwd.getBytes();
        String strEncryptionPwd = "";
        MessageDigest objGen;
        try {
            objGen = MessageDigest.getInstance("MD5");

            byte[] buffMD5 = objGen.digest(bytePwd);
            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < buffMD5.length; ++i) {
                int iVal = buffMD5[i] & 0xFF;
                String strHex = Integer.toHexString(iVal);

                if (i != 0) {
                    strBuilder.append("-");
                }
                if (strHex.length() < 2) {
                    strBuilder.append("0");
                }
                strBuilder.append(strHex);
            }
            strEncryptionPwd = strBuilder.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return strEncryptionPwd;
    }

    public static String getHashCode(Object object) throws IOException {
        if (object == null) {
            return "";
        }

        String ss = null;
        ObjectOutputStream s = null;
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            s = new ObjectOutputStream(bo);
            s.writeObject(object);
            s.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (s != null) {
                s.close();
                s = null;
            }
        }
        ss = getMd5(bo.toString());
        return ss;
    }

//    public static String sign(String text, String key) {
//        text = text + key;
//        return DigestUtils.md5Hex(getContentBytes(text, "utf-8"));
//    }
//
//    public static String sign(String text) {
//        return DigestUtils.md5Hex(getContentBytes(text, "utf-8"));
//    }

    /**
     * @param content
     * @param charset
     * @return
     * @throws SignatureException
     * @throws UnsupportedEncodingException
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }

    public static void main(String[] args) {
        System.out.println(getMd5("123456"));
    }
}
